#region Header Licence
//  NKnobMan, Copyright 2009, Alexandre MUTEL.
// 
//  This is free software; you can redistribute it and/or modify it
//  under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
// 
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public
//  License along with this software; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
//  02110-1301 USA, or see the FSF site: http://www.fsf.org.
#endregion
using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;

namespace NKnobMan
{
    [DataContract]
    public class LayerItem
    {
        private Layer _layer;

        protected LayerItem(Layer layer)
        {
            _layer = layer;

            PropertyInfo[] properties = GetType().GetProperties();
            foreach (PropertyInfo info in properties)
            {
                KnobManFieldAttribute knobManFieldAttribute =
                    Attribute.GetCustomAttribute(info, typeof(KnobManFieldAttribute)) as KnobManFieldAttribute;
                if (knobManFieldAttribute != null)
                {
                    if (info.PropertyType.Equals(typeof(IntAnimateLayerItem)) ||
                        info.PropertyType.Equals(typeof(FloatAnimateLayerItem)))
                    {
                        if ( knobManFieldAttribute.Name == null )
                        {
                            knobManFieldAttribute.SetDefault(info.Name);
                        }

                        object propInstance = Activator.CreateInstance(info.PropertyType, _layer, GetType().Name + "." + knobManFieldAttribute.Name, knobManFieldAttribute);
                        info.SetValue(this, propInstance, null);
                    }
                }
            }      
        }

        public Layer Layer
        {
            get { return _layer; }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            PropertyInfo[] properties = this.GetType().GetProperties();
            builder.Append("< ");
            foreach (PropertyInfo info in properties)
            {
                DataMemberAttribute dataMemberAttribute =
                    Attribute.GetCustomAttribute(info, typeof (DataMemberAttribute)) as DataMemberAttribute;
                if ( dataMemberAttribute != null)
                {
                    builder.Append(info.GetValue(this, null));
                    builder.Append(" ");
                }                
            }
            builder.Append(" >");
            return builder.ToString();
        }
    }
}